#include "comm.hpp"

int main()
{
    //1.获取唯一标识符k
    key_t fkid = ftok(P_PATH,PROJ_ID);
    if(fkid == -1)
    {
        perror("ftok");
        exit(1);
    }
    //2.获取共享内存
    int shmid = shmget(fkid,SIZE,0);
    if(shmid == -1)
    {
        perror("shmid");
        exit(2);
    }

    //3.挂连
    char* shmaddr = (char*)shmat(shmid,nullptr,0);

    //4.操作
    //直接往里写
    //直接进行内存级操作
    // while(true)
    // {
    //     //从键盘读取,把shmaddr直接看成一个字符串
    //     size_t s = read(0,shmaddr,sizeof(shmaddr));
    //     if(s > 0)
    //     {
    //         shmaddr[s-1] = 0;
    //         if(strcmp(shmaddr,"quit") == 0)
    //             break;
    //     }

    // }
    char* buffer = "i am a process";
    sprintf(shmaddr,"%s\n",buffer);

    //5.取消挂联
    int n = shmdt(shmaddr);

    return 0;
}